﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using mobil_lab1.localhost;

namespace mobil_lab1.Utilities
{
    class ArtistTreeView : System.Windows.Forms.TreeView
    {
        Element[] data;
        MusicWebService ws = new MusicWebService();

        public ArtistTreeView()
        {
            RetrieveData();
            BuildTree();
        }

        private void BuildTree()
        {
            TreeNode artistNode = new TreeNode("Artists");
            this.Nodes.Add(artistNode);
            Element EL = new Element();
            EL.Text = "Artists";
            EL.Title = "Artists";
            EL.Id = 0;
            artistNode.Tag = EL;
            TreeNode rootN = null;
            int rootID = 0;

            foreach (Element e in data)
            {
                if (e.ParentId == 0)
                {
                    rootN = new TreeNode();
                    artistNode.Nodes.Add(rootN);
                    rootN.Text = e.Title;
                    rootID = e.Id;
                    rootN.Tag = e;

                }
            }
        }

        public void ExpandNode(TreeNode node)
        {
            if (node == null || node.Nodes == null)
                return;

            node.Nodes.Clear();

            Element info = (Element)node.Tag;
            Element[] newNodes;

            newNodes = ws.GetByParentId(info.Id);

            foreach (Element e in newNodes)
            {
                TreeNode n = new TreeNode();
                n.Text = e.Title;
                n.Tag = e;
                node.Nodes.Add(n);
            }
            node.Expand();
        }

        private void RetrieveData()
        {
            data = ws.GetByParentId(0);
        }
    }
}
